#!/usr/bin/python3
# -*- coding: UTF-8 -*-

from app import App
from utils.tools import Tools
from time import time
import datetime
# from os import popen as osPopen, path as osPath  # 管道处理
import sys as osSys
import math
import json
import gol
import xml.etree.ElementTree as ET


gol._init()


class Application(App):
    # 初始配置
    def __init__(self):
        super().__init__()
        # 执行动作 [0 - 走路赚元宝: walk, 1 - 打工赚元宝: work, 2 - 摇一摇赚元宝: shake]
        self.iActionType = 2
        self.tupActionMap = {
            0: '走路赚元宝',
            1: '打工赚元宝',
            2: '摇一摇赚元宝'
        }
        # self.tool = Tools()
        self.iDisplayHeight = self._d.info.get('displayHeight') - 5
        self.iDisplayRight = self._d.info.get('displayWidth') - 100
        self.iDHB300 = self.iDisplayHeight - 300 # 底高300
        self.iHMove = 216 * 2 # 滚动高度bounds
        
    # ////////////---------------
    # nav 路径
    # 启动
    def startApp(self):
        self.tool.callApp('com.taobao.live/.home.activity.TaoLiveHomeActivity')
        
    
    # 关闭青少年模式弹窗
    def closePopupChild(self):
        self.tool.setSleep(2)
        self.el2CE('t', '我知道了')
        self.tool.setSleep(1)
        

    # 首页 - 我的
    def getWD(self):
        self.closePopupChild()
        bNode = self._d.xpath('//*[@resource-id="com.taobao.live:id/hp3_bottom_tab"]/android.widget.LinearLayout[1]/android.support.v7.app.ActionBar-Tab[4]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ImageView[1]').click_exists(timeout=2)
        self.myEcho("我的", bNode)
    
    # 关闭打工赚钱 - 弹窗
    def closeWYYDHZQ(self):
        self.tool.setSleep(2)
        bNode, oNode = self.el2('t', 'O1CN01WOnRmx1JJhubZYFtu_!!6000000001008-2-tps-560-656.png_')
        if bNode:
            self._d.xpath('//*[@resource-id="delivery-modal"]/android.view.View[2]/android.view.View[1]').click()
            self.tool.setSleep(1)
        # 新活动窗窗
        self._d(resourceId='com.taobao.live:id/tl_dx_card_container').child(className='android.widget.FrameLayout').child(className="android.widget.ImageView", index=3).click_exists(timeout=2)


    # 元宝中心
    def getYuBaoZhongXin(self):
        self.tapNode("t", "元宝中心")
        # 关闭弹窗
        self.closeWYYDHZQ()

    # 进入到元宝中心
    def toYuanBaoZhongxin(self, up=True):
        self.getWD()
        self.tool.setSleep(4)
        self.getYuBaoZhongXin()
        self.tool.setSleep(4)
        self.closeWYYDHZQ()
        if (up):
            self.tool.move([600, 900, 600, 400]) # 上滑一下，兼容底部浮层遮挡问题
        self.tool.setSleep(2)

    # ////////////---------------


    # ////////////---------------
    # 进入弹窗
    def pupupMore(self):
        self.el2CE('t','查看更多任务')

    # 三大任务
    # 走路赚钱
    def getWalk(self):
        self.iActionType = 0
        # 路径：元宝中心 - 走路赚元宝
        self.toNextNode('text', self.iActionType, True)
        self.tool.setSleep(2)
        self.el2CE('t','我知道了')
        self.tool.setSleep(1)
        # 左边 “领取” 按钮
        self.tapNode("t", "领取")
        self.pupupMore()
        # self.tool.setSleep(4)
        # 右边 “赚步数” 按钮
        self.toNextNode('icon', self.iActionType, True)
        self.tool.setSleep(1)
        if (self._d(text='你还有未使用步数').exists(timeout=2)):
            self.el2CE('t', '残忍离开')
            self.tool.setSleep(1)

    # 打工赚元宝
    def getWork(self):
        self.iActionType = 1
        # 路径：元宝中心 - 打工赚元宝
        self.toNextNode('text', self.iActionType, True)
        # 左边领体力 “去领取” 按钮
        self.tapNode('t', '去领取')
        self.pupupMore()
        # self.tool.setSleep(4)
        # 右边 “赚体力” 按钮
        self.toNextNode('icon', self.iActionType, True)
        self.tool.setSleep(1)

    # 摇一摇
    def getShake(self):
        self.iActionType = 2
        # 路径：元宝中心 - 打工赚元宝
        self.toNextNode('text', self.iActionType, True)
        # 右边 “赚次数” 按钮
        self.toNextNode('icon', self.iActionType, True)
        self.tool.setSleep(2)
        self.el2CE('t', '我知道了')
        self.tool.setSleep(1)
    # ////////////---------------


    '''
    # 跳下一个节点动作
    isClick = True
        return bNode
    isClick = False
        return bNode, oNode
    '''
    def toNextNode(self, sType, iIndex, isClick = False):
        iTmpIndex = int(iIndex)
        oMapList = [
            { # 赚步数
                'name': '走路赚元宝', 'type': 't',
                'value': 'O1CN01IeRzpJ1hSSJ53VxuH_!!6000000004276-2-tps-116-132.png_',
                'headerImg': 'O1CN010NIcmW264yPEFQS2o_!!6000000007609-2-tps-750-214.png_'
            },
            { # 赚体力
                'name': '打工赚元宝', 'type': 't',
                'value': 'O1CN01vWC7gg20DmKvWaURW_!!6000000006816-2-tps-248-246.png_',
                'headerImg': 'O1CN01ExSUrC1ovx8kzkMgT_!!6000000005288-2-tps-750-214.png_'
            },
            { # 赚次数
                'name': '摇一摇赚元宝', 'type': 'p',
                'value': '//*[@resource-id="page"]/android.view.View[2]/android.view.View[2]/android.view.View[1]/android.view.View[2]/android.view.View[1]',
                'headerImg': 'O1CN01il23fK1s4bT7kT7fL_!!6000000005713-2-tps-750-214.png_'
            },
        ]
        print(
            self.tupActionMap[iTmpIndex], '<->',
            oMapList[iTmpIndex]['name'], '<->',
            '菜单' if sType == 'text' else '按钮'
        )
        if (sType == 'text'):
            return self.el2CE("t", oMapList[iTmpIndex]['name']) if isClick else self.el2("t", oMapList[iTmpIndex]['name'])
        else:
            sTmpType = oMapList[iTmpIndex]['type']
            # 校验是否已经点开半屏弹窗 - 防误点
            oHeaderImg = self.el2("t", oMapList[iTmpIndex]['headerImg'])
            oIconImg = self.el2(sTmpType, oMapList[iTmpIndex]['value'])
            if (oHeaderImg[0]):
                return False
            if (isClick):
                bNode = oIconImg[1].click_exists(timeout=1)
                self.tool.setSleep(1)
                return bNode
            else:
                return oIconImg


    # 打工赚元宝 -> 去打工赚钱
    def getQDGZQ(self):
        print(">>> 去打工赚钱")
        self.tapNode("id", "action-main")
        self.tool.setSleep(2)
        bNode, = self.tapNode("t", "+888")
        if (bNode):
            self.tapNode("t", "开始打工")


    # 复用动作
    def toDo(self, anyFindTxt, sFindTxtSec, iTime, iType, callFN=None):
        iStartTime = time()
        # 匹配list节点（直播，视频）
        if (not sFindTxtSec):
            oClickNode = anyFindTxt
        else:
            oClickNode = self._d(textMatches=anyFindTxt).sibling(text=sFindTxtSec)
        bCanClick = oClickNode.exists(timeout=1)
        if bool(oClickNode) and bCanClick:
            oJsonInfo = json.loads(json.dumps(oClickNode.info))
            print('oJsonInfo', oJsonInfo)
            tupCenter = self.tool.center(oJsonInfo)
            if (tupCenter[1] < self.iDisplayHeight):
                self.tool.tap(tupCenter[0], tupCenter[1])
                if (iType == 2): # 视频类的上划
                    self.goRun(iTime)
                else: # 直播类直接等时间
                    iCount = iTime + 5
                    print("执行倒数：", end="")
                    self.tool.countDown(iCount)

                iEndTime = time()
                iLastTime = iTime - int(iEndTime - iStartTime)
                print(" 用时：%d 剩余时间：%d" % (int(iEndTime - iStartTime), iLastTime))
                self.tool.setSleep(1)
                self._d.press("back")
                self.tool.setSleep(1)
                self.el2CE('t', '残忍离开')
                self.tool.setSleep(3)
                return True
        return False


    # 重复动作
    def goRun(self, iTime, sType='upl'):
        sleepStep = 6
        i = math.ceil(iTime / sleepStep) + 3 # 多加2次上划补充卡顿问题造成的事故
        tmpType = sType
        while i > 0:
            # if (not sType):
            #     tmpType = 'upl' if i % 3 != 0 else 'downl'
            print("\r", end="")
            print("进度: {}: ".format(i), "▓" * (i), end="", flush = True)
            osSys.stdout.flush()
            self.tool.setSleep(sleepStep)
            pos = self.tool.moveDirection(tmpType)
            self.tool.move(pos)
            i -= 1

    # 兼容一下提前等退出事故
    def keepDo(self):
        bNode, oNode = self.tapNode('t', '继续做任务')
        # 反回前做一次检测
        if (bNode):
            self.goRun(10)
        else:
            self._d.press("back")


    # 迭代器 跑完视频项目
    def nextDo(self):
        # 查询关键词，次关键词，循环执行次数, 类型{1:直播， 2:视频}
        # 暂时无用
        # backUpTupList = (
        #     ("看黄金8点档直播30秒", 40, 10, 1),
        #     ("看直播10秒", "去完成", 14, 10, 1),
        #     ("发现好物60秒", "去完成", 70, 5, 2),
        #     ("看直播30秒", "去完成", 40, 10, 1),
        #     ("看视频30秒", "去完成", 40, 10, 2),
        #     ("看直播30秒", "去完成", 40, 10, 1),
        #     ("看直播3分钟", "去完成", 210, 3, 1),
        #     ("发现好物5分钟", "去完成", 380, 5, 2),
        #     ("看直播5分钟", "去完成", 380, 3, 1),
        #     ("看直播回放5分钟", "去观看", 330, 10, 1),
        #     ("看视频5分钟", "去完成", 330, 3, 2),
        #     ("看直播8分钟", "去完成", 550, 3, 1),
        #     ("看视频8分钟", "去完成", 550, 3, 2),
        #     ("看直播10分钟", "去完成", 680, 1, 1),
        #     ("看视频10分钟", "去完成", 680, 1, 2),
        # )
        tupListBefore = (
            ("看直播60秒", "去完成", 70, 10, 1),
            ("看直播90秒", "去完成", 100, 10, 1),
            ("看视频60秒", "去完成", 80, 10, 2),
            ("看视频60秒", "去观看", 80, 10, 2),
            ("看精彩内容3分钟", "去完成", 240, 5, 2),
            ("看发现好物3分钟", "去完成", 240, 5, 2),
            ("看发现好物2分钟", "去完成", 200, 1, 2),
            ("看视频2分钟", "去观看", 200, 1, 2),
            ("看视频2分钟", "去完成", 200, 1, 2),
        )
        tupListAfter = (
            ("看晚间视频60秒", "去完成", 80, 3, 2),
            ("看晚间惊喜视频60秒", "去完成", 80, 10, 2),
            ("看黄金8点档直播60秒", "去完成", 70, 10, 1),
            ("看黄金8点档直播60秒", "去观看", 70, 10, 1),
            ("看晚间精彩内容60秒", "去完成", 90, 10, 2),
            ("看晚间惊喜视频90秒", "去完成", 130, 10, 2),
            ("看黄金8点档直播90秒", "去完成", 100, 10, 1),
            ("看黄金8点档直播90秒", "去观看", 100, 10, 1),
            ("看视频90秒", "去完成", 130, 5, 2),
            ("看直播90秒", "去完成", 100, 10, 1),
            ("看黄金档直播并下单", "去完成", 130, 15, 1),
            ("看黄金8点档直播2分钟", "去完成", 130, 15, 1),
            ("看黄金8点档直播3分钟", "去完成", 190, 10, 1),
            ("看专享直降直播3分钟", "去完成", 190, 1, 2),
            ("看优价好物直播3分钟", "去完成", 190, 1, 2),
            ("看视频3分钟", "去完成", 240,5, 2),
        )
        
        iStart = int(self.iNextIndex) if self.iNextIndex else 0
        now = datetime.datetime.now()
        tupList = tupListBefore + tupListAfter
        if self.sAction == 'aa' or ('a' in self.sAction[2:]):
            if now.hour < 18:
                tupList = tupListBefore
            else:
                tupList = tupListAfter

        # itGo = iter(tupList[int(iStart):])
        for iIndex, tupItem in enumerate(tupList[int(iStart):]):
            tupItem = list(tupItem)
            print(self.tupActionMap[self.iActionType],  ' - ', iIndex, ' - ', tupItem)
            iStep = tupItem[3]

            while iStep > 0:
                # 调起半屏弹窗
                self.toNextNode('icon', self.iActionType, True)
                self.tool.setSleep(1)
                bToDo = self.reuseActions(tupItem[0], tupItem[1], tupItem[2], tupItem[4])
                # hack下app的bug
                if (self.iActionType == 0):
                    self._d.click(0.26, 0.199)
                    self.tool.setSleep(1)
                if (not bToDo):
                    iStep = 0
                    break
                iStep -= 1


    # 点赞20次
    def click20(self, sType):
        self.tool.setSleep(6)
        if (sType == 'walk'):
            self.hasZRWZBS()

        bNode = self._d(text="去直播间残暴点赞20次").sibling(text="去点赞").click_exists(timeout=2)
        if (bNode):
            self.tool.setSleep(6)
            i = 30
            while i > 0:
                self._d.double_click(700, 700, 0.2)
                print("点击", i)
                i -= 1
            self._d.press("back")


    def toMove(self, num):
        # 查询关键词，等待时间
        tupList = (
            ("浏览精选推荐30秒", "去浏览", 46),
            ("浏览上新日历", "去浏览", 46),
            ("浏览好货卖场30秒", "去完成", 46),
            ("浏览上新日历30秒", "去完成", 46),
            ("浏览精选推荐20秒", "去浏览", 36),
            ("浏览元宝商城30秒", "去浏览", 46),
            ("浏览天天特卖20秒", "去完成", 36),
            ("浏览店铺", "去完成", 46),
            ("看精彩内容60秒", "去完成", 90),
        )
        # itGo = iter(tupList)
        for iIndex, tupItem in enumerate(tupList):
            tupItem = list(tupItem)
            print(tupItem)
            if (self.iActionType == 0 or iIndex == 0):
                # 调起半屏弹窗
                self.toNextNode('icon', self.iActionType, True)
                if (num > 1):
                    self.tool.move([600, self.iDHB300, 600, self.iDHB300 - self.iHMove])
                    self.tool.setSleep(1)

            oClickNode = self._d(text=tupItem[0]).sibling(text=tupItem[1])

            bCanClick = oClickNode.exists
            if (bool(bCanClick)):
                oJsonInfo = json.loads(json.dumps(oClickNode.info))
                tupCenter = self.tool.center(oJsonInfo)
                if (tupCenter[1] < self.iDisplayHeight):
                    self.tool.tap(tupCenter[0], tupCenter[1])
                    self.tool.setSleep(3)
                    self.timeMove(tupItem[2])
                    if (num > 1):
                        self.tool.move([600, self.iDHB300 - self.iHMove, 600, self.iDHB300])
                        self.tool.setSleep(2)
                    self._d.press("back")
            
            if (num > 1):
                self.tool.move([600, self.iDHB300 - self.iHMove, 600, self.iDHB300])
                self.tool.setSleep(2)
        # 搜索商品或主播
        self.getSearchProductAndAnchor()


    # 重复上滑
    def timeMove(self, iTime):
        i = math.ceil(iTime / 6) + 4
        pos = self.tool.moveDirection("up")
        while i > 0:
            print("\r", end="")
            print("进度: {0}: ".format(i), "▓"*i, end="")
            osSys.stdout.flush()
            self.tool.setSleep(4)
            self.tool.move(pos)
            i -= 1


    # 幸运抽奖 
    def xycj(self):
        self._d.xpath('//*[@resource-id="page"]/android.view.View[4]/android.view.View[2]').click()
        self.tool.setSleep(8)
        bNode = self._d(text="继续抽奖").click_exists(timeout=2)
        if (bNode):
            self.xycj()


    # 循环领取走路金币
    def goWalkGold(self):
        i = 10
        while i > 0:
            self.getWalkGold()
            i -= 1


    # 领取走路元宝
    def getWalkGold(self):
        bNode, oNode = self.tapNode('tc', '8元宝')
        if (bNode):
            self.tool.setSleep(2)
            bNode, oNode = self.tapNode('tc', '额外领最高388')
            if (bNode):
                self.tool.countDown(80)
                self.el2CE('id', 'com.byted.pangle:id/tt_reward_full_count_down')


    # 走路赚完宝 打工赚元宝 - 上滑赚体力
    def justUp(self):
        self.timeMove(400)

    # 点淘鸭家园 - 种田重复点击
    def farmReDo(self):
        oCLEs = self._d(text='+6')
        tupPos = set({})
        # 不缓存会数据有问题
        for oView in oCLEs:
            oPos = oView.info['bounds']
            tupPos.add((oPos['left'], oPos['top']))

        for pos in tupPos:
            print(pos[0], pos[1])
            self._d.click(pos[0], pos[1])
            self.tool.setSleep(3)
            self.el2CE('t', '卷心菜')
            self.tool.setSleep(3)
        
        self.tool.setSleep(40 - len(tupPos) * 2)

    # 点淘鸭家园 - 种田动作
    def farm(self):
        self._d.swipe_ext("left", scale=0.8)
        self._d.swipe_ext("left", scale=0.8)
        # 是否已经调起弹窗
        # oPopup = self.el2('t', 'O1CN01TlFJY51WfmA0MdCbw_!!6000000002816-2-tps-311-73.png_')
        oPopup = self.el2('t', 'O1CN01u2jpRi1TOshUf9clT_!!6000000002373-2-tps-1500-556.png_')
        
        if (not oPopup[0]):
            oClickFarm = self.el2('t', '种子商店')
            if (oClickFarm[0]):
                oClickFarm[1].click_exists(timeout=1)
                self.tool.setSleep(2)
        # 下划点
        drowTop = oPopup[1].info['bounds']['bottom'] + 50
        # 下划一下到第一排
        self.tool.move([600, drowTop, 600, self.iDisplayHeight - 50])
        self.tool.setSleep(1)
        self.tool.move([600, drowTop, 600, self.iDisplayHeight - 50])
        self.tool.setSleep(1)
        self.tool.move([600, drowTop, 600, self.iDisplayHeight - 50])
        self.tool.setSleep(1)
        i = 30
        while i > 0:
            i -= 1
            self.farmReDo()

    # 点淘鸭家园 - 任务得金币
    def task(self):
        self.tool.setSleep(1)
        if (not self._d(text='O1CN01TTR5B828XUMclWSBH_!!6000000007942-2-tps-746-400.png_').exists):
            self.el2CE('t','O1CN014gUZhI1qRqOqWyG2s_!!6000000005493-2-tps-276-278.png_')
            self.tool.setSleep(1)
        if (self._d(text='浏览精选推荐30秒').sibling(text='去完成').click_exists(timeout=2)):
            self.tool.countDown(36, 'up')
            self._d(resourceId="J_dialogContainer").child(className='android.view.View').child(index=0, className='android.view.View').click_exists(timeout=1)
            self._d.press("back")
            self.el2CE('t', '开心收下')
            self.tool.setSleep(1)
        if (self.reuseActions('前往游戏中心', '去完成', 50, 1)):
            self._d.press("back")
            self.el2CE('t', '开心收下')
            self.tool.setSleep(1)
        tupList = (
            ("看直播60秒", "去完成", 65, 10, 1),
            ("看视频60秒", "去完成", 80, 10, 2),
            ("看好物视频60秒", "去完成", 80, 10, 2),
        )
        for iIndex, tupItem in enumerate(tupList):
            print(iIndex, ' - ', tupItem)
            iStep = tupItem[3]
            while iStep > 0:
                bToDo = self.reuseActions(tupItem[0], tupItem[1], tupItem[2], tupItem[4])
                if (not bToDo):
                    iStep = 0
                    break
                self.tool.setSleep(1)
                self.el2CE('t', '开心收下')
                self.tool.setSleep(1)
                iStep -= 1


    # 列表 - 签到60秒
    def signIn(self):
        self.tool.move([600, self.iDHB300, 600, self.iDHB300 - self.iHMove])
        self.tool.setSleep(1)
        self.tool.move([600, self.iDHB300, 600, self.iDHB300 - self.iHMove])
        if (self.el2CE('t', '今日签到')):
            # 等待一下弹窗
            self.tool.setSleep(4)
            oCheckNode = self.el2('t', '看直播60秒再得68元宝')
            if (not oCheckNode[0]):
                # 如果没有弹窗, 尝试点击‘开’字
                if (self.el2CE('t', 'O1CN01AqnTGR1k2IUAgzn0I_!!6000000004625-2-tps-254-263.png_')):
                    if (self.el2CE('t', '看直播60秒再得68元宝')):
                        self.tool.move([600, self.iDHB300 - self.iHMove, 600, self.iDHB300])
                        self.tool.setSleep(70)
                        self._d.press("back")
                        return
            self._d.press("back")


    # 右上角 - 领取奖励
    def topRightRewad(self):
        iStep = 3
        while iStep >= 0:
            if (self.el2CE('t', '领取奖励')):
                print('右角元宝:', iStep)
                if (self._d(text='邀请好友 再赚46元').exists(timeout=2)):
                    # 关闭弹窗
                    self.el2CE('t', 'O1CN01LxFPWH1Mmy2hurJW4_!!6000000001478-2-tps-54-54.png_')
                    return
                if (self.el2CE('t', '看直播60秒得68元宝')):
                    self.tool.countDown(70)
                    self._d.press("back")
        iStep -= 1
        print('DONE')


    # 打工赚元宝 - 右上角50体力
    def get50tl(self):
        self.iActionType = 1
        self.toNextNode('text', self.iActionType, True)
        if (self.el2CE('t', '浏览商城30秒送50体力')):
            self.tool.countDown(46, 'up')
            self._d.press("back")

    # -----------------------
    # 下单返购物金
    def getCashbackLoop(self, iIndex = 0):
        try:
            # 取两个循环对象
            oNode = self._d(text='看直播60秒得购物金')
            oNode = oNode[iIndex]
            iMax = oNode.sibling(index=6).get_text()[1:]
            iCurrent = oNode.sibling(index=5).get_text()
            iCurrent = int(iCurrent) if iCurrent.isnumeric() else 0
            while iCurrent < int(iMax):
                self.reuseActions(oNode, '', 65, 1)
                iCurrent += 1
            self.tool.setSleep(1)
        except KeyError:
            print('getCashbackLoop - error')

    def getCashback(self):            
        if (self.el2CE('t', '下单返购物金')):
            if (not self._d(text='O1CN01xzpsJ91IH8Ds8QZLd_!!6000000000867-2-tps-1500-596.png_').exists(timeout=2)):
                self.el2CE('t', '做任务赚购物金')
            else:
                self.tool.setSleep(2)
                self.el2CE('t', '去下单')
                self.tool.setSleep(2)
                self.el2CE('t', '去下单')

            self.tool.setSleep(1)
            self.getCashbackLoop(0)
            self.getCashbackLoop(1)
            self.tool.move([600, self.iDHB300, 600, self.iDHB300 - self.iHMove])
            self.reuseActions('看直播60秒', '去完成', 65, 1)
            self.el2CE('t', 'O1CN01qE4nNy1M45HMjL7jz_!!6000000001380-2-tps-60-60.png_')
            self.el2CE('t', '签到')
        self._d.press("back")
    # -----------------------

    # 搜索商品或主播
    def getSearchProductAndAnchor(self):
        if (self._d(text='搜索商品或主播').sibling(text='去完成').click_exists(timeout=2)):
            self.tool.setSleep(2)
            oNode = self._d(resourceId='com.taobao.live:id/taolive_search_history_words').child(className='android.widget.TextView')
            if (oNode[0].exists):
                oNode[0].click_exists(timeout=2)
                self.tool.setSleep(3)
                self._d.press("back")
            self._d.press("back")

    # 逛逛商城的真香秒杀
    def getFlashKilling(self):
        if (self._d(text='逛逛商城的真香秒杀').sibling(text='去完成').click_exists(timeout=2)):
            self.tool.setSleep(2)
            if (self._d(text='“真香秒杀”').exists):
                self.el2CE('t', '抢')
                self.tool.setSleep(2)
                self._d.press("back")
            self._d.press("back")

    # 滚动+1次
    def getOneDown(self):
        if (self._d(text='浏览页面30秒').sibling(text='+1次')):
            self.toUp(False)


class Action(Application):
    # 初始配置
    def __init__(self):
        super().__init__()

    def start(self,up=True):
        self.startApp()
        self.toYuanBaoZhongxin(up)

    def doNext(self, sAction):
        if ('wa' in sAction):
            self.getWalk()
        if ('wo' in sAction):
            self.getWork()
        if ('sh' in sAction):
            self.getShake()
        if ('l' in sAction):
            self.toMove(0)
            self.toMove(1)
            self.toMove(2)
            self.toMove(3)
        if ('v' in sAction):
            self.nextDo()

    def toUp(self, num, isStart=True):
        if (isStart):
            self.start()
            self.iActionType = num
            self.toNextNode('text', self.iActionType, True)
        self.tool.setSleep(3)
        self.tool.move([425, 1400, 460, 1100])
        if (self._d(text='浏览商品30秒').exists(timeout=2)):
            self.tool.move([425, 1400, 460, 100])
            self.tool.setSleep(2)
            self.timeMove(400)

    def toFarm(self):
        self.start(False)
        self.tool.setSleep(3)
        self.el2CE('t', '点淘鸭家园')
        self.tool.setSleep(3)
        self.farm()

    def all(self):
        self.start()
        self.get50tl()
        self.start(False)
        # self.signIn()
        self.start()
        self.doNext('sh-lv')
        self.start()
        self.doNext('wa-lv')
        self.start()
        self.doNext('wo-lv')
        self.start()
        self.getCashback()
        self.start(False)
        self.el2CE('t', '点淘鸭家园')
        self.task()


    '''
        wo-[look | video | auto]
        wo-l|v|a
        wa-l|v|a
        sh-l|v|a
    '''
    def init(self):
        sAction = self.sAction
        if ('-' in sAction):
            # self.start()
            self.doNext(sAction)
        elif (sAction == 'all'):
            print('all')
            self.all()
            self.all()
            self.start(False)
            self.topRightRewad()
            self.toUp(0)
            self.toUp(1)
            # self.toFarm()
        elif (sAction == 'aa'):
            print('auto')
            self.all()
            self.all()
            self.start(False)
            self.topRightRewad()
            self.toUp(0)
            self.toUp(1)
            # self.toFarm()
        elif (sAction == 'start'):
            self.start()
        elif (sAction == 'xycj'):
            self.xycj()
        elif (sAction == 'togold'):
            self.goWalkGold()
        elif (sAction == 'up'):
            self.toUp(0)
            self.toUp(1)
        elif (sAction == 'farm'):
            self.toFarm()
        elif (sAction == 'reward'):
            self.start(False)
            self.topRightRewad()
            self.signIn()
        elif (sAction == 'ot'):
            self.start(False)
            self.topRightRewad()
            self.signIn()
            # self.start()
            # self.getFlashKilling()
            self.toUp(0)
            self.toUp(1)
        elif (sAction == 'task'):
            # self.start(False)
            # self.el2CE('t', '点淘鸭家园')
            # self.el2CE('t', '开心收下')
            self.task()
        else:
            print('test')
            # print(self._d(text='今日浏览任务已完成').exists)
            # self.getFlashKilling()
            self.getCashback()

        return False

action = Action()
action.init()
print('DONE')
